//
//  ComposeViewController.swift
//  Weibo007
//
//  Created by apple on 15/7/6.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit
import FFAutoLayout

class ComposeViewController: UIViewController {

    /// 表情键盘控制器
    private lazy var emoticonKeyboardVC: EmoticonViewController = EmoticonViewController { [unowned self] (emoticon)  in
        self.textView.insertEmoticon(emoticon)
    }
    
    ///  切换表情视图
    func inputEmoticon() {
        // 如果输入视图是 nil，说明使用的是系统键盘
        print(textView.inputView)
        
        // 要切换键盘之前，需要先关闭键盘
        textView.resignFirstResponder()
        
        // 更换键盘输入视图
        textView.inputView = (textView.inputView == nil) ? emoticonKeyboardVC.view : nil
        
        // 重新设置焦点
        textView.becomeFirstResponder()
    }
    
    ///  选择照片
    func selectPicture() {
        print("选择照片")
    }
    
    ///  关闭
    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    ///  发布微博
    func sendStatus() {
        print(__FUNCTION__)
    }
    
    override func loadView() {
        view = UIView()
        
        view.backgroundColor = UIColor.whiteColor()
        
        // 1. 添加子控制器
        addChildViewController(emoticonKeyboardVC)
        
        prepareNavigationBar()
        prepareTextView()
    }
    
    private func prepareToolBar() -> UIToolbar {
        let tb = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 44))
        // 提示：设置一个和按钮背景一样的颜色，具体的数值，`平面设计师`会告诉我们
        tb.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        
        let itemSettings = [["imageName": "compose_toolbar_picture", "action": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        
        // 所有按钮的数组
        var items = [UIBarButtonItem]()
        
        // 设置按钮 - 问题：图片名称(高亮图片) / 监听方法
        for settings in itemSettings {
            items.append(UIBarButtonItem(imageName: settings["imageName"]!, highlightedImageName: nil, target: nil, actionName: settings["action"]))
            
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        // 删除末尾的弹簧
        items.removeLast()
        
        tb.items = items
        
        return tb
    }
    
    private func prepareTextView() {
        view.addSubview(textView)
        
        textView.text = "分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事..."
        textView.font = UIFont.systemFontOfSize(18)
        textView.ff_Fill(view)
        
        // 设置输入视图的辅助视图
        textView.inputAccessoryView = prepareToolBar()
        
        // 设置垂直滚动
        textView.alwaysBounceVertical = true
        // 滚动关闭键盘
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        
        // 设置 占位标签
        placeHolderLabel.text = "分享新鲜事..."
        placeHolderLabel.sizeToFit()
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 5, y: 8))
    }
    
    private func prepareNavigationBar() {
        // 1. 左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        // 2. 右侧按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        navigationItem.rightBarButtonItem?.enabled = false
        
        // 3. 导航栏
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 32))
        let label1 = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label1.text = "发微博"
        label1.sizeToFit()
        let label2 = UILabel(color: UIColor(white: 0.0, alpha: 0.5), fontSize: 13)
        label2.text = sharedUserAccount?.name ?? ""
        label2.sizeToFit()
        
        titleView.addSubview(label1)
        titleView.addSubview(label2)
        
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: titleView, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: titleView, size: nil)
        
        navigationItem.titleView = titleView
    }
    
    // MARK: 懒加载
    private lazy var textView = UITextView()
    private lazy var placeHolderLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 18)
    
}
